import request from "@/utils/request";
import type { ApiResponse, PageResult } from "@/types";

// 用户相关接口
export interface UserDTO {
  id: number;
  username: string;
  email: string;
  nickname?: string;
  avatar?: string;
  status: number;
  emailVerified: boolean;
  lastLoginTime?: string;
  createdAt: string;
  updatedAt: string;
  roles: string[];
  enabled: boolean;
  stats?: UserStats;
  profile?: UserProfileDTO;
}

export interface UserStats {
  postCount: number;
  commentCount: number;
  likeCount: number;
  followerCount: number;
  followingCount: number;
  whisperCount: number;
}

export interface UserProfileDTO {
  id: number;
  nickname?: string;
  avatar?: string;
  bio?: string;
  gender?: string;
  phone?: string;
  birthday?: string;
  createdAt: string;
  updatedAt: string;
}

export interface UserUpdateRequest {
  email?: string;
  nickname?: string;
  avatar?: string;
  bio?: string;
  gender?: string;
  phone?: string;
  birthday?: string;
}

/**
 * 获取当前登录用户详细信息
 */
export function getCurrentUser(): Promise<UserDTO> {
  return request({
    url: "/users/me",
    method: "get",
  });
}

/**
 * 根据用户ID获取用户详情
 */
export function getUserDetail(userId: number): Promise<UserDTO> {
  return request({
    url: `/users/${userId}`,
    method: "get",
  });
}

/**
 * 获取用户统计数据
 */
export function getUserStats(userId: number): Promise<UserStats> {
  return request({
    url: `/users/${userId}/stats`,
    method: "get",
  });
}

/**
 * 更新用户资料
 */
export function updateUserProfile(data: UserUpdateRequest): Promise<ApiResponse> {
  return request({
    url: "/users/me",
    method: "put",
    data,
  });
}

/**
 * 获取用户列表
 */
export function getUsers(params?: {
  page?: number;
  size?: number;
  keyword?: string;
  status?: number;
}): Promise<PageResult<UserDTO>> {
  return request({
    url: "/users",
    method: "get",
    params,
  });
}

/**
 * 搜索用户
 */
export function searchUsers(params: {
  keyword: string;
  page?: number;
  size?: number;
}): Promise<PageResult<UserDTO>> {
  return request({
    url: "/users/search",
    method: "get",
    params,
  });
}

/**
 * 关注用户
 */
export function followUser(userId: number): Promise<ApiResponse> {
  return request({
    url: `/users/${userId}/follow`,
    method: "post",
  });
}

/**
 * 取消关注用户
 */
export function unfollowUser(userId: number): Promise<ApiResponse> {
  return request({
    url: `/users/${userId}/follow`,
    method: "delete",
  });
}

/**
 * 获取用户粉丝列表
 */
export function getUserFollowers(
  userId: number,
  page = 1,
  size = 10
): Promise<PageResult<UserDTO>> {
  return request({
    url: `/users/${userId}/followers`,
    method: "get",
    params: { page, size },
  });
}

/**
 * 获取用户关注列表
 */
export function getUserFollowing(
  userId: number,
  page = 1,
  size = 10
): Promise<PageResult<UserDTO>> {
  return request({
    url: `/users/${userId}/following`,
    method: "get",
    params: { page, size },
  });
}

/**
 * 检查是否关注了某个用户
 */
export function getFollowStatus(userId: number): Promise<boolean> {
  return request({
    url: `/users/${userId}/follow-status`,
    method: "get",
  });
}
